package com.xiaomai.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaomai.eduservice.entity.EduChapter;
import com.xiaomai.eduservice.entity.EduVideo;
import com.xiaomai.eduservice.entity.chapter.ChapterVo;
import com.xiaomai.eduservice.entity.chapter.VideoVo;
import com.xiaomai.eduservice.mapper.EduChapterMapper;
import com.xiaomai.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaomai.eduservice.service.EduVideoService;
import com.xiaomai.servicebase.config.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-08-30
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

        @Autowired
        private EduVideoService eduVideoService;
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //查询章节
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();//创建出查询章节表的条件对象
        chapterQueryWrapper.eq("course_id",courseId);//设置条件-字段名couese_id的值等于传过来的courseId
        List<EduChapter> eduChapterList = baseMapper.selectList(chapterQueryWrapper);//根据条件查询数据库

        //查询小节
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();//创建出查询小节表的条件对象
        videoQueryWrapper.eq("course_id",courseId);
        List<EduVideo> eduVideoList = eduVideoService.list(videoQueryWrapper);//查询数据库得到courseId相同的小节集合
        //封装章节
        List<ChapterVo> finalList = new ArrayList<>();//创建一个装章节的集合

        for (EduChapter eduChapter : eduChapterList) {  //遍历章节集合
            ChapterVo chapterVo = new ChapterVo();      //创建一个章节对象
            BeanUtils.copyProperties(eduChapter,chapterVo);//将遍历到的章节信息通过工具类赋值给chapterVo
            finalList.add(chapterVo);  //将chapterVo添加进集合

            List<VideoVo> videoVoList = new ArrayList<>();//创建一个装小节的集合
            for (EduVideo eduVideo : eduVideoList) {
                if(eduVideo.getChapterId().equals(chapterVo.getId())){//如果小节的章节id和章节的id相同
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoVoList.add(videoVo);//则将小节放进同一个集合中
                }
            }
            chapterVo.setChildren(videoVoList);//再将集合放进对应的章节里
        }

        //封装小节

        return finalList;//把最后的集合返回
    }

    //根据id删除章节
    @Override
    public boolean deleteById(String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(wrapper); //判断该章节是否还有小节
        if (count>0){//如果有的话则报异常
            throw new GuliException(200001,"删除数据失败");
        }else{//没有就删除
            int result = baseMapper.deleteById(chapterId);
            return result>0;
        }
    }

    //
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
